package com.yuan.mail.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @Author SiYuan
 * @Description :
 * @Date 2018/11/30 16:11
 */
@SuppressWarnings("ALL")
@Component
public class MailServiceImpl implements MailService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JavaMailSender mailSender;

    @Value("${mail.fromMail.addr}")
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try {
         mailSender.send(message);
            //处理发送失败的消息
            logger.info("简单邮件已经发送。");
        } catch (Exception e) {
            // 添加到消息队列中,使用定时器发送 todo
            logger.error("发送简单邮件时发生异常！", e);
        }

    }
}
